package org.example.demo.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.example.demo.entity.MeetingMinutes;
import org.example.demo.service.MeetingMinutesService;
import org.example.demo.util.AjaxResult;
import org.example.demo.util.ReturnMap;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("meeting")
public class MeetingMinutesController {

    @Resource
    private MeetingMinutesService meetingMinutesService;

    // 分页查询会议记录列表
    @PostMapping("selectPage")
    public AjaxResult selectPage(@RequestBody MeetingMinutes meeting,
                                 @RequestParam(defaultValue = "1") Integer pageNum,
                                 @RequestParam(defaultValue = "10") Integer pageSize) {
        Map<String, Object> map = new HashMap<>();
//        ReturnMap returnMap = new ReturnMap();
        Page<MeetingMinutes> page = new Page<>(pageNum, pageSize);

        QueryWrapper<MeetingMinutes> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda()
                .like(meeting.getTitle() != null && !meeting.getTitle().isEmpty(), MeetingMinutes::getTitle, meeting.getTitle())
                .orderByDesc(MeetingMinutes::getId);
        Page<MeetingMinutes> result = meetingMinutesService.page(page, queryWrapper);

        map.put("page", result);
//        returnMap.setData("page", result);
        return AjaxResult.success(map);
    }

    // 查询会议记录总数
    @PostMapping("selectCount")
    public AjaxResult selectCount(@RequestBody MeetingMinutes meeting) {

        Map<String, Object> map = new HashMap<>();
//        ReturnMap returnMap = new ReturnMap();
        QueryWrapper<MeetingMinutes> queryWrapper = new QueryWrapper<>();
        int count = meetingMinutesService.count(queryWrapper.lambda().like(meeting.getTitle() != null && !meeting.getTitle().isEmpty(), MeetingMinutes::getTitle, meeting.getTitle()));

        map.put("count", count);
//        returnMap.setData("count", count);
        return AjaxResult.success(map);
    }

    // 新增/修改会议记录
    @PostMapping("saveOrUpdate")
    public AjaxResult saveOrUpdate(@RequestBody MeetingMinutes meeting) {
//        ReturnMap returnMap = new ReturnMap();
//        Map<String, Object> map = new HashMap<>();
        if (meeting.getId() != null) {
            meeting.setUpdateTime(LocalDateTime.now());
            meetingMinutesService.updateById(meeting);
        } else {
            meeting.setCreateTime(LocalDateTime.now());
            meeting.setUpdateTime(LocalDateTime.now());
            meetingMinutesService.save(meeting);
        }

//        returnMap.setReturnCode(200);
        return AjaxResult.success();
    }

    // 查询会议记录详情
    @GetMapping("selectById")
    public AjaxResult selectById(@RequestParam Integer id) {

        Map<String, Object> map = new HashMap<>();
        ReturnMap returnMap = new ReturnMap();
        MeetingMinutes meeting = meetingMinutesService.getById(id);

        map.put("meeting", meeting);
//        returnMap.setData("meeting", meeting);
        return AjaxResult.success(map);
    }

    // 删除会议记录
    @DeleteMapping("delete")
    public AjaxResult delete(@RequestParam Integer id) {
//        ReturnMap returnMap = new ReturnMap();
        boolean success = meetingMinutesService.removeById(id);
//        returnMap.setReturnCode(success ? 200 : 405);
        if (success) return AjaxResult.success();
        return AjaxResult.error();
//        return returnMap.getreturnMap();
    }

}